/*=============================================================================

Public Replication File for:

Alan S. Gerber, Gregory A. Huber, Albert H. Fang, and Catlan E. Reardon

"The effect on turnout of campaign mobilization messages addressing ballot
secrecy concerns: A replication experiment"

(Forthcoming, PLOS ONE)

Last updated: 21 July 2017

=============================================================================*/
clear all
set more off, permanently

* install packages 
ssc install outreg2
net install outsum

log using Analysis.log , replace

use PublicReplicationData.dta, clear

/*----------------------------
 Calculate IPWs
----------------------------*/

* Calculate IPWs by sample definition
foreach s in sdef {
preserve
contract treat if `s'==1, freq(freq) percent(pct)
gen ipw_`s' = 1/(pct/100)
list, t noobs
keep treat ipw_`s'
save ipw_`s'.dta, replace
restore
}

* Merge back into main analysis file
foreach s in sdef {
merge m:1 treatment using ipw_`s'.dta
drop _merge
label var ipw_`s' "IPW for sample definition `s'==1"
}

* Delete temporary datasets
foreach s in sdef {
erase ipw_`s'.dta
}


/*----------------------------
Table 1 and Table A3
Estimate treatment effects
----------------------------*/

foreach s in sdef {

*Weighted, with covariates
reg vote2014 treatment d_* age_miss days_miss if `s'==1 [aweight=ipw_`s'], robust
qui sum vote2014 [aweight=ipw_`s'] if treat==0 & e(sample)
local c_turnout = r(mean)
outreg2 using "Table1_and_TableA3_RegressionEstimates.xls", replace ctitle("Weighted and, With Covariates") label dec(5) ///
	addstat(Control Group Mean Turnout, `c_turnout') ///
	addtext("Weighted?", Yes, "With Covariates?", Yes)

*Weighted, without covariates
reg vote2014 treatment if `s'==1 [aweight=ipw_`s'], robust
qui sum vote2014 [aweight=ipw_`s'] if treat==0 & e(sample)
local c_turnout = r(mean)
outreg2 using "Table1_and_TableA3_RegressionEstimates.xls", append ctitle("Weighted and, Without Covariates") label dec(5) ///
	addstat(Control Group Mean Turnout, `c_turnout') ///
	addtext("Weighted?", Yes, "With Covariates?", No)

*Unweighted, with covariates
reg vote2014 treatment d_* age_miss days_miss if `s'==1, robust
qui sum vote2014 if treat==0 & e(sample)
local c_turnout = r(mean)
outreg2 using "Table1_and_TableA3_RegressionEstimates.xls", append ctitle("Unweighted and, With Covariates") label dec(5) ///
	addstat(Control Group Mean Turnout, `c_turnout') ///
	addtext("Weighted?", No, "With Covariates?", Yes)

*Unweighted, without covariates
reg vote2014 treatment if `s'==1, robust
qui sum vote2014 if treat==0 & e(sample)
local c_turnout = r(mean)
outreg2 using "Table1_and_TableA3_RegressionEstimates.xls", append ctitle("Unweighted and, Without Covariates") label dec(5) ///
	addstat(Control Group Mean Turnout, `c_turnout') ///
	addtext("Weighted?", No, "With Covariates?", No)

}


/*----------------------------
Table A1
Randomization checks
----------------------------*/

reg treatment d_* age_miss days_miss if sdef==1 [aweight=ipw_sdef]
testparm *
local fstat = round(r(F), 0.01)
local p = round(r(p), 0.01)
outreg2 using "TableA1_RandomizationCheck.xls", ctitle("Weighted") label addstat("F-statistic", `fstat', "F-stat p-value", `p') dec(3) replace

reg treatment d_* age_miss days_miss if sdef==1
testparm *
local fstat = round(r(F), 0.01)
local p = round(r(p), 0.01)
outreg2 using "TableA1_RandomizationCheck.xls", ctitle("Unweighted") label addstat("F-statistic", `fstat', "F-stat p-value", `p') dec(3) append

/*----------------------------
Table A2
Balance table
----------------------------*/

outsum d_* age_miss days_miss [aweight=ipw_sdef] if sdef==1 & treatment == 0 using "TableA2_BalanceTable.xls", ctitle("Control") replace bracket addnote("Cells present weighted means and weighted standard deviations.")
outsum d_* age_miss days_miss [aweight=ipw_sdef] if sdef==1 & treatment == 1 using "TableA2_BalanceTable.xls", ctitle("Treatment") append bracket 

log close
